// miniprogram/pages/my/my_address/add/index.js
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    name: "",
    phone: "",
    address: "",
    houseNo: "",
    label: "",
    defaultAddress: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var param={};
    app.postRequest("/rest/customer/my.htm", param, this.backViewData);

  },
  backViewData(res) {
    if (res.code == 0) {
      if(res.avatar){
        var datas=[];
        datas.push({url:res.avatar});
        this.setData({fileList:datas});
      }
      this.setData({
        name: res.name,
        phone: res.phone,
        address: res.address,
        label:res.address,
        avatar: res.avatar
      });
    }
  },
  afterRead: function (event) {
    var self=this;
    const { file } = event.detail;
    // 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
    wx.uploadFile({
      url: 'https://cloud.newbyte.ltd/storage/upload.htm', // 仅为示例，非真实的接口地址
      filePath: file.url,
      name: 'file',
      formData: { user: 'test' },
      success(res) {
      var str=  res.data;
      var obj=JSON.parse(str);
        // 上传完成需要更新 fileList
        const { fileList = [] } = self.data;
        fileList.push({ url: obj.url });
        self.setData({ fileList });
      },
    });
  },
  deleteImage(e){
    console.info(e);
    const index = e.detail.index;
    var datas=  this.data.fileList;
    datas.splice(index,1);
    this.setData({fileList:datas});

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  selectAddress: function () {
    var self = this;
    wx.chooseLocation({
      success: function (res) {
        console.log(res);
        if (res.name) {
          self.setData({
            label: res.name,
            address: res.address
          });
          self.data.lat = res.latitude;
          self.data.lng = res.longitude;
        }
      },
      fail: function (error) {
        console.error(error);
      },
      complete: function (res) {
        console.log(res);
      }
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  isEmpty(str) {
    if (str == undefined || str == "") {
      return true;
    } else {
      return false;
    }
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  saveAddress: function () {
    console.info(this.data);
    var params = this.data;
    var logos=this.data.fileList;
    if(logos==null||logos.length==0){
      app.showModal("请上传个人头像")
      return;
    }
    params.avatar=logos[0].url;
    if (this.isEmpty(params.name)) {
      app.showModal("请填写用户名称")
      return;
    }
    if (this.isEmpty(params.phone)) {
      app.showModal("请填写联系电话")
      return;
    }
    if (this.isEmpty(params.label)) {
      app.showModal("请选择定位")
      return;
    }
    if (this.isEmpty(params.address)) {
      app.showModal("请填写详情地址")
      return;
    }
    app.postRequest("/rest/customer/updateMy.htm", params, this.backData);
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  backData: function (res) {
    if (res.code == 0) {
      app.showModal("更新成功", () => {
        wx.navigateBack({
          delta: 1,
          success: (res) => {},
          fail: (res) => {},
          complete: (res) => {},
        })
      });
    } else {
      app.showModal(res.msg);
    }
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  changeState: function (e) {
    this.data.defaultAddress = e.detail.value;
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})